package com.xh.bussiness.thirdapi.base.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.xh.bussiness.permission.model.User;
import com.xh.bussiness.permission.service.IUserService;
import com.xh.bussiness.thirdapi.base.bean.dto.ThirdClassLessonDto;
import com.xh.bussiness.thirdapi.base.bean.request.QueryThirdClassLessonRequest;
import com.xh.bussiness.thirdapi.xuebang.dto.VO.XuebangResultVO;
import com.xh.bussiness.thirdapi.xuebang.dto.request.QueryXuebnagClassLessonRequest;
import com.xh.bussiness.thirdapi.xuebang.model.XuebangClassLesson;
import com.xh.bussiness.thirdapi.xuebang.service.IXuebangService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.properties.XhProperties;
import com.xh.core.util.StringUtils;
import com.xh.core.util.UserUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Pitt Cheung
 * @date 2021/7/9 10:18
 */
@RestController
@RequestMapping("/third/tms")
public class BaseThridTMSController {


    @Autowired
    private XhProperties xhProperties;
    @Autowired
    private IXuebangService xuebangService;
    @Autowired
    private IUserService userService;

    @RequestMapping("/queryThirdClassLessonData")
    @ApiOperation("查询第三方教务班级排课数据")
    public AjaxResult queryThirdClassLessonData(QueryThirdClassLessonRequest request) {
        String currentUserId = UserUtils.getCurrentUserId();
        User user = userService.getById(currentUserId);
        if (user == null) {
            return new AjaxResult(AjaxResultConstant.Result.ERROR, "当前用户不存在");
        }
        List<ThirdClassLessonDto> resultDatas = new ArrayList<>();
        //对接教务系统数据来源（主要查询班级排课数据：1：新舟TMS；2：飞鸿TMS；3:学邦TMS；4：校宝TMS；5：校管家TMS）
        Integer tmsSource = xhProperties.getTmsSource();
        if (tmsSource.equals(1)) {

        } else if (tmsSource.equals(2)) {

        } else if (tmsSource.equals(3)) {
            QueryXuebnagClassLessonRequest xuebnagClassLessonRequest = new QueryXuebnagClassLessonRequest();
            xuebnagClassLessonRequest.setTeacherId(Integer.parseInt(user.getThirdId()));
            xuebnagClassLessonRequest.setCourseDateBegin(request.getCourseDateBegin());
            xuebnagClassLessonRequest.setCourseDateEnd(request.getCourseDateEnd());
            if (StringUtils.isNotEmpty(request.getThirdClassId())) {
                xuebnagClassLessonRequest.setMiniClassId(Integer.parseInt(request.getThirdClassId()));
            }
            xuebnagClassLessonRequest.setPageSize(500);
            XuebangResultVO resultVO = xuebangService.queryXuebangClassLesson(xuebnagClassLessonRequest);

            if (resultVO != null && resultVO.getData() != null) {
                List<Object> datas = resultVO.getData().getDatas();
                if (CollectionUtil.isNotEmpty(datas)) {
                    List<ThirdClassLessonDto> collect = datas.stream().map(itme -> {
                        XuebangClassLesson xuebangClassLesson = BeanUtil.toBean(itme, XuebangClassLesson.class);
                        ThirdClassLessonDto dto = ThirdClassLessonDto.builder()
                                .lessonId(xuebangClassLesson.getMiniClassCourseId().toString())
                                .lessonName(xuebangClassLesson.getMiniClassName())
                                .courseDate(xuebangClassLesson.getCourseDate())
                                .courseTime(xuebangClassLesson.getCourseTime())
                                .miniClassId(xuebangClassLesson.getMiniClassId() == null ? null : xuebangClassLesson.getMiniClassId().toString())
                                .miniClassName(xuebangClassLesson.getMiniClassName())
                                .teacherId(xuebangClassLesson.getTeacherId() == null ? null : xuebangClassLesson.getTeacherId().toString())
                                .teacherName(xuebangClassLesson.getTeacherName())
                                .build();
                        return dto;
                    }).collect(Collectors.toList());

                    resultDatas.addAll(collect);
                }
            }
        } else if (tmsSource.equals(4)) {

        } else if (tmsSource.equals(5)) {

        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, resultDatas);
    }

}
